# Author    : CoffeeChicken
# Date      : 2022/1/3 11:17
# Function  : 演示 绘制人口医院数对比折线图

import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.globals import ThemeType
import json
import requests

population = [2189, 1387, 7461, 3492, 2405, 4259, 2407, 3185, 2487, 8475, 6457, 6103, 4154, 4519, 10153, 9937, 5775,
              6644, 12601, 5013, 1008, 3205, 8367, 3856, 4721, 365, 3953, 2502, 592, 720, 2585]

province = ["北京",
            "天津",
            "河北",
            "山西",
            "内蒙古",
            "辽宁",
            "吉林",
            "黑龙江",
            "上海",
            "江苏",
            "浙江",
            "安徽",
            "福建",
            "江西",
            "山东",
            "河南",
            "湖北",
            "湖南",
            "广东",
            "广西",
            "海南",
            "重庆",
            "四川",
            "贵州",
            "云南",
            "西藏",
            "陕西",
            "甘肃",
            "青海",
            "宁夏",
            "新疆"]

response = requests.get("http://192.168.134.161:8081/hospitals")

hospitals = json.loads(response.text)['hospitals']

all_beds = {}
for hospital in hospitals:
    if hospital['bedNum'] != -1:
        all_beds[hospital['province']] = all_beds.get(hospital['province'], 0) + int(hospital['bedNum'])

province_bed_num = []
for p in province:
    province_bed_num.append(all_beds[p])

print(province_bed_num)
# x = [list(z) for z in zip(province, population)]
# print(x)

line_chart = (
    Line(init_opts=opts.InitOpts(width="1400px",
                                height="600px"))
        .extend_axis(yaxis=opts.AxisOpts(name="床位数量", type_="value", position="right"))
        .extend_axis(yaxis=opts.AxisOpts(name="人口数量(万人)", type_="value", position="left"))
        .add_xaxis(xaxis_data=province)
        .add_yaxis(series_name="人口数量", stack="总量", y_axis=population, linestyle_opts=opts.LineStyleOpts(width=4),
                   yaxis_index=0)
        .add_yaxis(series_name="床位数量", stack="总量", y_axis=province_bed_num, linestyle_opts=opts.LineStyleOpts(width=4),
                   yaxis_index=1)
        .set_global_opts(
        title_opts=opts.TitleOpts(title="人口床位对比图"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        yaxis_opts=opts.AxisOpts(
            type_="value",
            axistick_opts=opts.AxisTickOpts(is_show=True),
            splitline_opts=opts.SplitLineOpts(is_show=True),
        ),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
    )
        .render("population_bed_num.html")
)
